﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using EPiServer.Core;
using EPiServer.PlugIn;
using EPiServer.ServiceLocation;

namespace EPiBoost.Core.Plugins.Events
{
	public class TrimContentNameOnPublish : PlugInAttribute
	{
		public static void Start()
		{
			if (Configuration.Core.Plugins.TrimContentNameOnPublish.Enabled)
			{
				ServiceLocator.Current.GetInstance<IContentEvents>().PublishingContent += Instance_PublishingContent;
				//EPiServer.DataFactory.Instance.PublishingContent += Instance_PublishingContent;
			}
		}

		static void Instance_PublishingContent(object sender, EPiServer.ContentEventArgs e)
		{
			if (e.RequiredAccess == EPiServer.Security.AccessLevel.NoAccess)
				return; 

			TrimPageName(e.Content);
		}

		private static void TrimPageName(IContent content)
		{
			content.Name = content.Name.Trim();
		}

	}
}
